package com.platform.tony.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.platform.tony.ext.UserExt;
import com.platform.tony.entity.Role;
import com.platform.tony.entity.User;
import com.platform.tony.mapper.UserMapper;
import com.platform.tony.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserExt queryUserInfo(String openId) {
        return null;
    }

    @Override
    public UserExt queryUserInfoByPhone(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getCellphone, phone);
        User user = userMapper.selectOne(wrapper);
        UserExt ext = BeanUtil.copyProperties(user, UserExt.class);
        if(ObjectUtil.isNotEmpty(user)){
            ext.setRoleList(Lists.newArrayList(new Role("ADMIN"), new Role("USER")));
        }
        return ext;
    }

    @Override
    public UserExt queryUserInfoByEmail(String email) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getEmail, email);
        User user = userMapper.selectOne(wrapper);

        UserExt ext = BeanUtil.copyProperties(user, UserExt.class);
        if(ObjectUtil.isNotEmpty(user)){
            ext.setRoleList(Lists.newArrayList(new Role("ADMIN"), new Role("USER")));
        }
        return ext;
    }


}
